package org.example.configcenter;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class FetchWithHttpClient {

    public static String fetch(String url, String token) throws Exception {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // GET 请求
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Authorization", "Bearer " + token);

            // POST 请求（示例）
            // HttpPost httpPost = new HttpPost(url);
            // httpPost.setHeader("Authorization", "Bearer " + token);
            // httpPost.setEntity(new StringEntity("{\"key\":\"value\"}"));

            return EntityUtils.toString(httpClient.execute(httpGet).getEntity());
        }
    }
}
